package org.hk.kg.finance.student.service;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.hk.kg.db.DBContext;
import org.hk.kg.utils.Constants;
import org.hk.kg.utils.LocalResouce;

public class StudentService implements IStudentService {
	private static final String INSRT_STUDENT_1_SQL = 
			"insert into student (stuId,stuName,stuClz,stuType,stuSex,birthDate,enterDate,photo) values"
			+"('${studentId}','${studentName}',${stuClz},'${stuType}','${studentSex}',${birthDate},${enterDate},'${photo}')",
			
			
			QRY_STUDENT_SQL = "select stu.stuId stuId,"
				+ "stu.stuName stuName,"
				+ "stu.stuClz stuClz," 
				+ "clz.stuClzName clzName,"
				+ "stu.stuType stuType,"
				+ "type.typeName typeName,"
				+ "stuSex from student stu,stu_clz clz,stu_type type"
				+ " where stu.stuClz=clz.stuClzId and stu.stuType=type.typeId"
				+ " $notNull(stuType,and stuType=${stuType})"
				+ " $notNull(stuClz,and stuClz=${stuClz})"
				+ " $notNull(stuId,and stuId=${stuId})"
				+ " $notNull(stuName,and stuName like '%${stuName}%')";
	public boolean createStudent(Map infoMap) {
		//将照片保存
		String photoPath = "" + infoMap.get("photo"),
			stuClz = "" + infoMap.get("stuClz"),
			stuId = "" + infoMap.get("studentId"),
			rootPath = LocalResouce.fileJoin(Constants.IMAGE_ROOT, Constants.CUSTOM_IMAGE);
		File dir = new File(rootPath);
		if(!dir.exists())
		{
			dir.mkdirs();
		}
		File clzFolder = new File(LocalResouce.fileJoin(rootPath, stuClz));
		if(!clzFolder.exists())
		{
			clzFolder.mkdirs();
		}
		
		File imageSave = new File(clzFolder,stuId + ".jpg"),
			imageSrc = new File(photoPath);
		try {
			LocalResouce.fileCopy(imageSrc, imageSave,true);
		} catch (IOException e) {
			return false;
		}
		return DBContext.instatnce.execute(Constants.DB_DEFAULT, INSRT_STUDENT_1_SQL, infoMap) == 1;
	}
	public List<Map> queryStudents(Map qryMap) {
		return DBContext.instatnce.query(Constants.DB_DEFAULT, QRY_STUDENT_SQL, qryMap);
	}

}
